package yl.hs.bmipfull.service.laboratorydata.imple;


import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.laboratorydata.ILabConfigDesignReportDao;
import yl.hs.bmipfull.daos.laboratorydata.ILabConfigDesignReportRecordsDao;
import yl.hs.bmipfull.daos.laboratorydata.ILabConfigDesignReportRmsDao;
import yl.hs.bmipfull.daos.production.IProductionTaskDao;
import yl.hs.bmipfull.pojo.laboratorydata.*;
import yl.hs.bmipfull.service.laboratorydata.ILabConfigDesignRecordService;
import yl.hs.bmipfull.service.laboratorydata.ILabConfigDesignReportService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.SearchFilterEngine;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author 吴高丰
 * @apiNote 配比设计报告信息服务层接口实现
 * @since 2022年07月19日 16时11分27秒 <br/>
 */
@Service
public class LabConfigDesignReportServiceImpl extends LaboratorydataBaseService implements ILabConfigDesignReportService {

    @Autowired
    ILabConfigDesignReportDao dao;
    @Autowired
    ILabConfigDesignReportRecordsDao Recordsdao;
    @Autowired
    ILabConfigDesignReportRmsDao rmsDao;
    @Autowired
    IProductionTaskDao taskDao;

    @Override
    public List<LabConfigDesignReportEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(LabConfigDesignReportEntity.class);
        pageQuery.getQuery().put("sortKey", String.join(",", pageQuery.getSortSides()));
        pageQuery.getQuery().put("unitSNArray", getCompanySNList());
        var filters = SearchFilterEngine.generateQueryList(pageQuery.getFilters(), LabConfigDesignReportEntity.class);
        return dao.queryList(pageQuery.getQuery(), filters);
    }

    @Override
    public PageResult<LabConfigDesignReportEntity> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(LabConfigDesignReportEntity.class);
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        PageResult<LabConfigDesignReportEntity> result = new PageResult(rowNum);
        var filters = SearchFilterEngine.generateQueryList(pageQuery.getFilters(), LabConfigDesignReportEntity.class);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery(), filters));
        result.setPage(page);
        result.setData(dao.queryPage(pageQuery.getQuery(), filters));
        return result;
    }


    @Override
    public LabConfigDesignReportEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer saveLabConfigDesignReport(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;
        LabConfigDesignReportEntity entity = null;
        LabConfigDesignReportRmsEntity rmsentity = null;
        LabConfigDesignReportRecordsEntity repotrentity = null;
        var snn = "";
        ObjectMapper objectMapper = new ObjectMapper();
        if (Tl.isEmpty(sn)) {
            //执行保存
            entity = new LabConfigDesignReportEntity();
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setSn(queryNewSN(LabConfigDesignReportEntity.class));
            result = dao.insertNew(entity);
            if (result > 0) {
                writeLog(ActionType.CREATE, entity.getSn(), "插入配比设计报告信息信息", Tl.toJson(entity), LabConfigDesignReportEntity.class);
                taskDao.updateGenerateTeport(entity.getTaskSN(),true);
            }
            //var ddd = model.get("subData");
            if (model.get("subData")!=null && model.get("subData")!="") {
                var subData = (List<Map<String, Object>>) model.get("subData");
                for (var item : subData) {
                    rmsentity = new LabConfigDesignReportRmsEntity();
                    Tl.getChangeLog(rmsentity, item);
                    rmsentity.setPYFieldValue();
                    rmsentity.setReportSN(entity.getSn());
                    rmsentity.setRecordSN(entity.getConfigRecordSN());
                    result = rmsDao.insertNew(rmsentity);
                    if (result > 0) {
                        writeLog(ActionType.CREATE, entity.getSn(), "插入报告配比明细信息", Tl.toJson(rmsentity), LabConfigDesignReportRmsEntity.class);
                    }
                }
            }
            var ccc = model.get("formItem");
            if (model.get("formItem") != null && ccc!="") {
                var formItem = (List<Map<String, Object>>) model.get("formItem");
                for (var item : formItem) {
                    repotrentity = new LabConfigDesignReportRecordsEntity();
                    Tl.getChangeLog(repotrentity, item);
                    repotrentity.setPYFieldValue();
                    repotrentity.setReportSN(entity.getSn());
                    repotrentity.setConfigRecordSN(entity.getConfigRecordSN());
                    result = Recordsdao.insertNew(repotrentity);
                    if (result > 0) {
                        writeLog(ActionType.CREATE, entity.getSn(), "插入报告试件实验信息", Tl.toJson(repotrentity), LabConfigDesignReportRecordsEntity.class);
                    }
                }
            }
        } else {
            //执行修改
            entity = findBySn(sn);
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            result = dao.update(entity);
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, "更新配比设计报告信息信息", Tl.toJson(entity), LabConfigDesignReportEntity.class);
            }
            rmsDao.deleteBySn(sn);
            var subData = (List<Map<String, Object>>) model.get("subData");
            for (var item : subData) {
                rmsentity = new LabConfigDesignReportRmsEntity();
                Tl.getChangeLog(rmsentity, item);
                rmsentity.setPYFieldValue();
                rmsentity.setReportSN(entity.getSn());
                rmsentity.setRecordSN(entity.getConfigRecordSN());
                result = rmsDao.insertNew(rmsentity);
                if (result > 0) {
                    writeLog(ActionType.CREATE, entity.getSn(), "插入报告配比明细信息", Tl.toJson(rmsentity), LabConfigDesignReportRmsEntity.class);
                }
            }
            Recordsdao.deleteBySn(sn);
            var formItem = (List<Map<String, Object>>) model.get("formItem");
            for (var item : formItem) {
                repotrentity = new LabConfigDesignReportRecordsEntity();
                Tl.getChangeLog(repotrentity, item);
                repotrentity.setPYFieldValue();
                repotrentity.setReportSN(entity.getSn());
                repotrentity.setConfigRecordSN(entity.getConfigRecordSN());
                result = Recordsdao.insertNew(repotrentity);
                if (result > 0) {
                    writeLog(ActionType.CREATE, entity.getSn(), "插入报告试件实验信息", Tl.toJson(repotrentity), LabConfigDesignReportRecordsEntity.class);
                }
            }
        }
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer delete(String sn) {
        Integer result = dao.changeStateBySNS(sn, DataState.DELETED.code);
        if (result > 0) {
            writeLog(ActionType.DELETE, sn, LabConfigDesignReportEntity.class);
        }
        return result;
    }
}